package com.dt.web.excel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.util.ListUtils;
import com.dt.system.domain.ModRepositoriesBox;
import com.dt.system.domain.ModRepositoriesLayer;
import com.dt.system.domain.ModRepositoriesShelves;
import com.dt.system.service.IModRepositoriesBoxService;
import com.dt.system.service.IModRepositoriesLayerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * 直接用map接收数据
 *
 * @author yxg
 */
@Component
public class RepositoriesBoxDataListener extends AnalysisEventListener<Map<Integer, String>> {

    private static IModRepositoriesLayerService repositoriesLayerService;
    private static IModRepositoriesBoxService repositoriesBoxService;


    @Autowired
    public void setService(IModRepositoriesBoxService repositoriesBoxService){
        RepositoriesBoxDataListener.repositoriesBoxService=repositoriesBoxService;
    }

    @Autowired
    public void setService(IModRepositoriesLayerService repositoriesLayerService){
        RepositoriesBoxDataListener.repositoriesLayerService=repositoriesLayerService;
    }

    /**
     * 每隔5条存储数据库，实际使用中可以100条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 100;
    private List<Map<Integer, String>> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

    @Override
    public void invoke(Map<Integer, String> data, AnalysisContext context) {
        cachedDataList.add(data);
        if (cachedDataList.size() >= BATCH_COUNT) {
            saveData();
            cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        saveData();
        System.out.println("所有数据解析完成！");
    }

    /**
     * 加上存储数据库
     */
    private void saveData() {



        List<ModRepositoriesBox> boxList=new ArrayList<>();
        //遍历所有行数
        for (Map<Integer,String> item: cachedDataList) {
            //创建对象
            ModRepositoriesBox modRepositoriesBox=new ModRepositoriesBox();

            modRepositoriesBox.setBoxNo(item.get(5));
            modRepositoriesBox.setFileUseNum(Long.parseLong(item.get(6)));



            //查询该盒子所属层格

            ModRepositoriesLayer modRepositoriesLayer=new ModRepositoriesLayer();
            modRepositoriesLayer.setLayerNo(item.get(4));
            List<ModRepositoriesLayer> lst1=repositoriesLayerService.selectModRepositoriesLayerList(modRepositoriesLayer);
            if(!lst1.isEmpty())
            {
                modRepositoriesBox.setShelvesId(lst1.get(0).getShelvesId());
                modRepositoriesBox.setLayerId(lst1.get(0).getId());
                modRepositoriesBox.setRepositoriesId(lst1.get(0).getRepositoriesId());
            }



            //查询数据是否存在，如果不存在，则新增，存在则忽略
            ModRepositoriesBox temp=new ModRepositoriesBox();
            temp.setBoxNo(item.get(5));
            List<ModRepositoriesBox> rblst = repositoriesBoxService.selectModRepositoriesBoxList(temp);
            if(rblst==null || rblst.isEmpty()) {

                boxList.add(modRepositoriesBox);

            }
        }
        if(!boxList.isEmpty()){
            repositoriesBoxService.batchInsertModRepositoriesBox(boxList);
        }

        }

    }